import Request from './request.js';
import util from './util.js';
let request = new Request().http

export default {
	acceptShare(type, option) {
		if (type == 1) {
			//不带id
			var url = decodeURIComponent(option.q);
			if (url != null && url != "" && url != "undefined") {
				var list = url.split("?");
				if (list != null && list.length > 1) {
					var t = list[1].split("&");
					//设置分享人cardNo
					this.setParentCardNo(t[0].split("=")[1]);
				}
			}
			var scene = decodeURIComponent(option.scene);
			if (scene != null && scene != "" && scene != "undefined") {
				var list = scene.split(",");
				if (list != null && list.length > 0) {
					//设置分享人cardNo
					this.setParentCardNo(list[0]);
				}
			}
			let parentCardNo = option.cardNo;
			if (parentCardNo != null && parentCardNo != "" && parentCardNo != "undefined") {
				//设置分享人cardNo
				this.setParentCardNo(parentCardNo);
			}
		} else if (type == 2) {
			//返回id
			let id = option.id;
			var url = decodeURIComponent(option.q);
			if (url != null && url != "" && url != "undefined") {
				var list = url.split("?");
				if (list != null && list.length > 1) {
					let t = list[1].split("&");
					id = t[0].split("=")[1];
					//设置分享人cardNo
					this.setParentCardNo(t[1].split("=")[1]);
				}
			}
			var scene = decodeURIComponent(option.scene);
			if (scene != null && scene != "" && scene != "undefined") {
				var list = scene.split(",");
				if (list != null && list.length == 1) {
					id = list[0];

				} else if (list.length > 1) {
					id = list[0];
					//设置分享人cardNo
					this.setParentCardNo(list[1]);
				}
			}
			let parentCardNo = option.cardNo;
			if (parentCardNo != null && parentCardNo != "" && parentCardNo != "undefined") {
				//设置分享人cardNo
				this.setParentCardNo(parentCardNo);
			}
			return id;
		} else if (type == 3) {
			//设备二维码
			let id = option.id;
			var url = decodeURIComponent(option.q);
			if (url != null && url != "" && url != "undefined") {
				var list = url.split("?");
				if (list != null && list.length > 1) {
					id = list[1].split("=")[1];
				}
			}
			var scene = decodeURIComponent(option.scene);
			if (scene != null && scene != "" && scene != "undefined") {
				var list = scene.split(",");
				if (list != null && list.length > 1) {
					id = list[0];
				}
			}
			let parentCardNo = option.cardNo;
			if (parentCardNo != null && parentCardNo != "" && parentCardNo != "undefined") {
				//设置分享人cardNo
				this.setParentCardNo(parentCardNo);
			}
			return id;
		}
	},
	//获取重新加载
	initReload: function(successFn) {
		// #ifdef APP-PLUS
		let reload = uni.getStorageSync('reload');
		if(util.isNotEmpty(reload)){
			uni.setStorage({
				key: 'reload',
				data: '',
				success: function() {
					if (typeof successFn == 'function') {
						successFn();
					}
				}
			});
		}
		// #endif
		// #ifndef APP-PLUS
		let comPages = getCurrentPages();
		let comCurrPage = comPages[comPages.length - 1];
		if (util.isNotEmpty(comCurrPage.data) && util.isNotEmpty(comCurrPage.data.reload)) {
			if (typeof successFn == 'function') {
				successFn();
			}
		}
		// #endif
		
	},
	//设置重新加载
	setReload: function() {
		// #ifdef MP-WEIXIN
		
		var pages = getCurrentPages();
		var currPage = pages[pages.length - 1]; //当前页面
		var prevPage = pages[pages.length - 2]; //上一个页面
		prevPage.$vm.reload = 'reload';
		uni.navigateBack({
			delta: 1,
			success() {},
			fail() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			}
		});
		// #endif
		// #ifndef MP-WEIXIN
		uni.setStorage({
			key: 'reload',
			data: 'reload',
			success: function() {
				uni.navigateBack();
			}
		});
		// #endif
	},
	//获取分享信息
	getShare: function(res, title, pathUrl, imageUrl) {
		let members = this.getMembers();
		if (util.isNotEmpty(members) && util.isNotEmpty(members.cardNo) && util.isNotEmpty(
				pathUrl)) {
			if (pathUrl.indexOf("?") > -1) {
				pathUrl = pathUrl + '&cardNo=' + members.cardNo;
			} else {
				pathUrl = pathUrl + '?cardNo=' + members.cardNo;
			}
		}
		if (util.isEmpty(imageUrl)) {
			return {
				title: title,
				path: pathUrl
			}
		} else {
			return {
				title: title,
				imageUrl: imageUrl,
				path: pathUrl
			}
		}
	},
//设置电影院
	setCinema: function(cinema, successFn) {
		uni.setStorage({
			key: 'cinema',
			data: cinema,
			success: function() {
				if (typeof successFn == 'function') {
					successFn();
				}
			}
		});
	},
	//获取电影院
	getCinema: function() {
		let cinema = uni.getStorageSync('cinema');
		return cinema;
	},
	//设置座位信息
	setSeat: function(seat, successFn) {
		uni.setStorage({
			key: 'seat',
			data: seat,
			success: function() {
				if (typeof successFn == 'function') {
					successFn();
				}
			}
		});
	},
	//获取座位
	getSeat: function() {
		let seat = uni.getStorageSync('seat');
		return seat;
	},
	//获取学生id
	getStudentId: function() {
		let studentId = uni.getStorageSync('studentId');
		return studentId;
	},
	//设置学生id
	setStudentId: function(studentId,successFn) {
		uni.setStorage({
			key: 'studentId',
			data: studentId,
			success: function() {
				if (typeof successFn == 'function') {
					successFn();
				}
			}
		});
	},
	//获取分享人parentCardNo
	getParentCardNo: function() {
		let token = uni.getStorageSync('parentCardNo');
		return token;
	},
	//设置分享人parentCardNo
	setParentCardNo: function(parentCardNo) {
		uni.setStorage({
			key: 'parentCardNo',
			data: parentCardNo
		});
	},
	//设置审核模式
	getAuditStatus: function() {
		let data = uni.getStorageSync("auditStatus");
		if (data == 1) {
			return 1;
		} else {
			return 0;
		}
	},
	getUrlContent: function(id, success) {
		if (util.isNotEmpty(id) && id.indexOf("URL") === 0) {
			//转换类型
			let _self = this;
			this.getUrlById({
				id: id
			}).then(res => {
				let content = res.data.content;
				let t = content.split(",");
				if (t.length > 1) {
					//设置分享人cardNo
					this.setParentCardNo(t[1]);
				}
				success(t[0]);
			});
		} else {
			success(id);
		}
	},
	//设置审核模式
	setAuditStatus: function(auditStatus, successFn) {
		uni.setStorage({
			key: 'auditStatus',
			data: auditStatus,
			success: function() {
				if (typeof successFn == 'function') {
					successFn();
				}
			}
		});
	},
	
	//token失效
	getTokenOutSave: function() {
		let data = uni.getStorageSync("tokenOutSave");
		if (data === null || data === '' || data === undefined || data === 'undefined' || data === 'null') {
			return false;
		} else {
			return true;
		}
	},
	//token失效
	setTokenOutSave: function(tokenOut, successFn) {
		uni.setStorage({
			key: 'tokenOutSave',
			data: tokenOut,
			success: function() {
				if (typeof successFn == 'function') {
					successFn();
				}
			}
		});
	},
	
	//获取存储值
	getStorage: function(key) {
		let keyValue = uni.getStorageSync(key);
		return keyValue;
	},
	//设置存储值
	setStorage: function(keyName, keyValue) {
		uni.setStorage({
			key: keyName,
			data: keyValue
		});
	},
	//获取token
	getToken: function() {
		let token = uni.getStorageSync('token');
		return token;
	},
	//设置token
	setToken: function(token, successFn) {
		uni.setStorage({
			key: 'token',
			data: token,
			success: function() {
				if (typeof successFn == 'function') {
					successFn();
				}
			}
		});
	},
	
	//获取members
	getMembers: function() {
		let members = uni.getStorageSync('members');
		return members;
	},
	//设置members
	setMembers: function(members,successFn) {
		uni.setStorage({
			key: 'members',
			data: members,
			success: function() {
				if (typeof successFn == 'function') {
					successFn();
				}
			}
		});
	},
	//获取City
	getCity: function() {
		let city = uni.getStorageSync('city');
		return city;
	},
	//设置City
	setCity: function(city,successFn) {
		uni.setStorage({
			key: 'city',
			data: city,
			success: function() {
				if (typeof successFn == 'function') {
					successFn();
				}
			}
		});
	},
	//获取权限
	getAuthorize: function(scope, showMsg, fnSuccess, fnFail) {
		uni.authorize({
			scope: scope,
			success() { //1.1 允许授权
				fnSuccess();
			},
			fail() { //1.2 拒绝授权
				uni.showModal({
					title: '',
					content: showMsg,
					confirmText: '立即授权',
					cancelText: '取消授权',
					success: function(res) {
						if (res.confirm) {
							uni.openSetting({
								success(res) {
									fnSuccess();
								}
							});
						} else {
							if (typeof fnFail === "function" && fnFail()) {
								fnFail()
							}
						}
					}
				});
			}
		})
	},
	//地址权限
	chooseLocation: function(showMsg, fnSuccess) {
		this.getAuthorize('scope.userLocation', showMsg, function() {
			uni.chooseLocation({
				success: function(res) {
					var regex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;
					var REGION_PROVINCE = [];
					var addressBean = {
						REGION_PROVINCE: null,
						REGION_COUNTRY: null,
						REGION_CITY: null,
						ADDRESS: null
					};

					function regexAddressBean(address, addressBean) {
						regex = /^(.*?[市州]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;
						var addxress = regex.exec(address);
						addressBean.REGION_CITY = addxress[1];
						addressBean.REGION_COUNTRY = addxress[2];
						addressBean.ADDRESS = addxress[3];
						console.log(addxress);
					}
					if (!(REGION_PROVINCE = regex.exec(res.address))) {
						regex = /^(.*?(省|自治区))(.*?)$/;
						REGION_PROVINCE = regex.exec(res.address);
						addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
						regexAddressBean(REGION_PROVINCE[3], addressBean);
					} else {
						addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
						regexAddressBean(res.address, addressBean);
					}

					fnSuccess(res.name, res.address, res.latitude, res.longitude, addressBean);
				},
				fail: function(res) {
					console.log(JSON.stringify(res).toString());
				}
			});
		});
	},
	//获取token
	getJumpLogin: function(type) {
		if (type == "2") {
			//提示登录
			uni.showModal({
				title: '',
				content: '请先登录/注册',
				showCancel: false,
				success: function(res) {

				}
			});
		} else if (type == "3") {
			uni.showModal({
				title: '',
				content: '您还未登录，请前往我的页面进行登录/注册',
				success: function(res) {
					uni.redirectTo({
						url: "/pages/my/my",
					})
				}
			});
		} else if (type == "4") {
			uni.showModal({
				title: '',
				content: '您还未授权，请先授权',
				success: function(res) {
					if (res.confirm) {
						uni.navigateTo({
							url: "/pages/login/index",
						})
					} else if (res.cancel) {
						uni.navigateBack()
					}

				}
			});
		} else {
			uni.redirectTo({
				url: "/pages/my/my?loginTip=1",
			})
		}

	},
	//经纬度
	getLocation: function(showMsg, fnSuccess, fnFail) {
		this.getAuthorize('scope.userLocation', showMsg, function() {
			uni.getLocation({
				type: 'wgs84',
				success: function(res) {
					console.log('当前位置的经度：' + res.longitude);
					console.log('当前位置的纬度：' + res.latitude);
					fnSuccess(res.longitude, res.latitude);
				}
			});

		}, function() {
			if (typeof fnFail === "function" && fnFail()) {
				fnFail()
			}
		});
	},
	//获取默认数据,type:1、首页数据
	getStorageData: function(name) {
		return uni.getStorageSync(name);
	},
	//设置默认数据
	setStorageData: function(name, data) {
		uni.setStorage({
			key: name,
			data: data
		});
	},
	//获取config
	getConfig: function(successFn) {
		let config = uni.getStorageSync('config');
		if (config != null && config != "") {
			successFn(config);
		} else {
			this.getWebConfig().then(res => {
				if (res.code == 0) {
					uni.setStorage({
						key: 'config',
						data: res.data
					});
					successFn(res.data);
				}
			});
		}
	},
	//获取参数相关数据
	getWebConfig: function() {
		return request('/api/phone/base/selectWxConfig', 'GET');
	},
	//根据code获取昵称和头像信息
	getWxMemberByCode: function(data) {
		return request('/api/phone/base/getWxMemberByCode', data, 'POSTS');
	},

	//创建或查询用户信息
	createToken: function(data) {
		return request('/api/phone/base/getWebMemberByOpenid', data, 'POSTS');
	},
	//手机号创建或查询用户信息
	createTokenPhone: function(data) {
		return request('/api/phone/base/getWebMemberByOpenidPhone', data, 'POSTS');
	},
	//获取小程序手机号
	getXcxPhone: function(data) {
		return request('/api/phone/base/getXcxPhone', data, 'POSTS');
	},
	//更新用户信息
	updateUser: function(data) {
		return request('/api/phone/base/updateWebMember', data, 'POSTS');
	},
	//txet配置接口接口
	selectSysConfigText: function(data) {
		return request('/api/phone/base/selectSysConfigText', data, 'GET');
	},
	//我的账单分页
	myWxScoreRecordPage: function(data) {
		return request('/api/phone/base/myWxScoreRecordPage', data, 'POSTS');
	},
	//查询我的提现分页
	myWxWithdrawPage: function(data) {
		return request('/api/phone/base/myWxWithdrawPage', data, 'POSTS');
	},
	//反馈
	feedback: function(data) {
		return request('/api/phone/base/feedback', data, 'POSTS');
	},
	//查询当前用户金额状况
	getCurrentMemberMoney: function(data) {
		return request('/api/phone/base/getCurrentMemberMoney', data, 'GET');
	},
	//获取微信菜单
	wxMenuList: function(data) {
		return request('/api/phone/base/wxMenuList', data, 'GET');
	},
	//提现保存
	webWithdrawUpdate: function(data) {
		return request('/api/phone/base/webWithdrawUpdate', data, 'POSTS');
	},
	//公司区域接口
	listSysUserArea: function(data) {
		return request('/api/phone/home/listSysUserArea', data, 'GET');
	},
	//个人信息接口
	selectMemberByToken: function(data) {
		return request('/api/phone/base/getByToken', data, 'GET');
	},
	//获取分享二维码
	getShareEwm: function(data) {
		return request('/api/phone/base/getShareEwm', data, 'GET');
	},
	//短码id获取长码内容
	getUrlById: function(data) {
		return request('/api/phone/base/getUrlById', data, 'POSTS');
	},
	//url转换为短码
	changeUrl: function(data) {
		return request('/api/phone/base/changeUrl', data, 'POSTS');
	},
	//获取分享二维码图片
	getEwmShareImage: function(data) {
		return request('/api/phone/base/getEwmShareImage', data, 'POSTS');
	},
	//我推荐的用户
	myWxMemberRecommendPage: function(data) {
		return request('/api/phone/base/myWxMemberRecommendPage', data, 'POSTS');
	},
	//用户分享页面数据
	shareData: function(data) {
		return request('/api/phone/base/shareData', data, 'GET');
	},
	//获取菜单按钮
	wxMenuList: function(data) {
		return request('/api/phone/base/wxMenuList', data, 'GET');
	},
	//获取富文本内容
	getSysConfigDataByKeyword: function(data) {
		return request('/api/phone/base/getSysConfigDataByKeyword', data, 'POSTS');
	},
	//根据dictType获取集合
	listByDictCodes: function(data) {
		return request('/api/phone/base/listByDictCodes', data, 'GET');
	},
	
	//图片上传
	fileUpload: function(baseUrl, tempFilePath, successFn) {
		uni.showLoading({
			title: '图片上传中',
			mask: true
		})
		uni.uploadFile({
			url: baseUrl + '/api/phone/base/fileUploadOnlyImage', //仅为示例，非真实的接口地址
			filePath: tempFilePath,
			name: 'file',
			header: {
				"token": uni.getStorageSync('token'),
			},
			success: (uploadFileRes) => {
				if (typeof uploadFileRes.data == "string") {
					uni.hideLoading();
					let result = JSON.parse(uploadFileRes.data);
					successFn(result);

				} else {}
			}
		});
	}



}
